<template>
  <div>
      <page-main class="searchMain">
            <!-- 搜索 -->
            <div class="searchBox">
                <span>搜索</span>
                <el-input v-model="searchValue" class="search" @keyup.enter="searchInput" placeholder="请输入ID 单位名称或者监管等级"></el-input>
            </div>
        </page-main>
    <page-main>
      <el-table
        :data="
          tableData.filter(
            (data) =>
              !search || data.name.toLowerCase().includes(search.toLowerCase())
          )
        "
        style="width: 100%;"
        :default-sort="{prop: 'id', order: 'ascending'}"
      >

        <el-table-column label="单位名称" prop="full_name"> </el-table-column>
        <!-- <el-table-column label="监管等级" prop="watch_level"> </el-table-column> -->
        <el-table-column label="单位地址" prop="address"> </el-table-column>
        <el-table-column label="单位类型" prop="name"> </el-table-column>
        <el-table-column label="负责人" prop="contact_name"> </el-table-column>
        <el-table-column label="联系电话" prop="phone"> </el-table-column>
        <el-table-column width="300" label="操作">
          <template #header>
            <el-button size="mini" type="primary" @click="addEditSocialUnit">添加</el-button>
          </template>
          <template #default="scope">
            <div>
              <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row)"
                >修改</el-button
              >
               <el-popconfirm
                confirm-button-text="删除"
                cancel-button-text="取消"
                icon="el-icon-info"
                icon-color="red"
                title="确定删除？"
                @confirm="handleDelete(scope.$index, scope.row)"
                @cancel="cancelEvent">
                <template #reference>
                  <el-button size="mini" type="danger">删除</el-button>
                </template>
              </el-popconfirm>
            </div>
            <div style="margin-top: 10px;">
              <el-button
                size="mini"
                type="primary"
                @click="childMange(scope.$index, scope.row)"
                >子单位管理</el-button
              ><el-button
                size="mini"
                type="success"
                @click="userMange(scope.$index, scope.row)">用户管理</el-button>

            </div>
            <div style="margin-top: 10px;">
                <el-button type="info"  size="mini" @click="patrol(scope.$index, scope.row)">
                  巡查点
              </el-button>
              <el-button
                size="mini"
                @click="constructMange(scope.$index, scope.row)"
                type="info" plain
                >建筑管理</el-button
              >
            </div>
            <div style="margin-top: 10px;">
                <el-button type="success" plain  size="mini" @click="qrcode(scope.$index, scope.row)">
                  二维码管理
              </el-button>
              <el-button type="danger" plain  size="mini" @click="toTaskList(scope.$index, scope.row)">
                  任务巡查
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @current-change="handleCurrentChange"
          v-model:currentPage="tableDataInfo.page"
          :page-count="tableDataInfo.total_page"
          :total="tableDataInfo.total"
          layout="total,prev, pager, next, jumper"
          background
        >
          <!-- :hide-on-single-page="true" -->
        </el-pagination>
      </div>
    </page-main>
  </div>
</template>
<script>
import storage from "@/util/storage";
import { ElMessage } from "element-plus";
export default {
  data() {
    return {
        searchValue: '',
      tableData: [],
      search: "",
      tableDataInfo: "",
    };
  },
  created() {
    this.getDataList();
  },

  methods: {
    getDataList(page) {
      let pageNum = 1;
      if (page) {
        pageNum = page;
      }
      let user_info = this.$store.state.user.user_info;
      this.$api
        .post("/unit/UnitInformation/lists", {
          region_id: user_info.region_id,
          page: pageNum,
          rows: 6,
        })
        .then((res) => {
          this.tableData = res.data.lists;
          this.tableDataInfo = res.data;
        });
    },
    // 搜索
    searchInput() {
        this.getDataList()
    },
    handleEdit(index, row) {
      this.$router.push({
        name: "editSocialUnit",
        query: { id: row.id },
      });
    },
    handleDelete(index, row) {
      this.$api.post("/unit/UnitInformation/delete", { id: row.id }).then((res) => {
          if (res.code === 200) {
              this.$message({
                    type: 'success',
                    message: res.msg,
                })
          } else {
              this.$message({
                    type: 'error',
                    message: res.msg,
                })
          }
        let tableData = this.tableData;
        tableData.splice(index, 1);
        this.getDataList();
      });
    },

    handleSizeChange(e) {},
    handleCurrentChange(e) {
      this.getDataList(e);
    },
    closeDialog() {
      this.$refs.addForm.resetFields();
    },
    addEditSocialUnit() {
      this.$router.push({
        name: "addSocialUnit",
      });
    },
    childMange(index, row) {
      this.$router.push({
        name: "sociolUnitChildPage",
        query: { unit_id: row.id },
      });
    },
    userMange(index, row) {
      this.$router.push({name:'sociolUserPage',
      query:{
          id:row.id//单位id
      }})
    },
    patrol(index,row){
        this.$router.push({name:'patrolPoint',query:{
            id:row.id
        }})
    },
    constructMange(index,row){
        this.$router.push({name:'constructionPage',query:{
            id:row.id
        }})
    },
    qrcode(index,row){
        this.$router.push({name:'qrcode',query:{
            id:row.id
        }})
    },
    toTaskList(index,row) {
      localStorage.removeItem('TASK_FROM');
      this.$router.push({
        name:'sociolUnitTaskMange',
        query: {
          unit_id:row.id
        }
      })
    }
  },
};
</script>
<style scoped lang="scss">
.block {
    display: flex;
    justify-content: center;
}
.searchBox {
    display: flex;
    span {
        width: 50px;
        line-height: 40px;
        text-align: center;
    }
    .search {
        width: 30%;
    }
}
</style>
